package com.hack4jp.hackearthquakes;

import java.util.List;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.location.Address;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class LocationMapOverlay extends Overlay implements OnDoubleTapListener,OnGestureListener {

	private GestureDetector _gesture = null;  
	private LocationMapActivity _parentAct;
	private GeoPoint _currentLocation;
    private Bitmap _iconLocation = null;
    private Point _offsetIcon = null;

	LocationMapOverlay(LocationMapActivity act){ 		
		_gesture = new GestureDetector(this);
		_gesture.setOnDoubleTapListener(this);
		
		_parentAct = act;
		_currentLocation = null;
	}
	
	@Override  
	public boolean onTouchEvent(MotionEvent e, MapView mapView) {  
		 _gesture.onTouchEvent(e);  
		 return super.onTouchEvent(e, mapView);  
	}
	
	public void setLocationIcon(Bitmap icon) {
		_iconLocation = icon;
		_offsetIcon = new Point();
		_offsetIcon.x = 0 - _iconLocation.getWidth() / 2;
		_offsetIcon.y = 0 - _iconLocation.getHeight() / 2;
	}
	
	public void setLocationPoint(GeoPoint gp) {
		_currentLocation = gp;
		setCurrentLocationText();
	}
	
	public void setCurrentLocationText()
	{
		try{
			List<Address> list_address = _parentAct.geo().getFromLocation(
					LocationMapActivity.getGeocoderDouble(_currentLocation.getLatitudeE6()),
					LocationMapActivity.getGeocoderDouble(_currentLocation.getLongitudeE6()),
					1);
			if (!list_address.isEmpty()){
				Address address = list_address.get(0);
				StringBuffer sbAddress = new StringBuffer();
				
				int count = 0;
				while(true) {
					String addr = address.getAddressLine(count++);
					if (addr == null)
						break;
					
					if (sbAddress.length() == 0) {
						if (addr.equals("日本") || addr.equals("Japan"))
							continue;
					}
					
					if (sbAddress.length() > 0)
						sbAddress.append(" ");
					
					sbAddress.append(addr);
				}

				_parentAct.setLocationText(sbAddress.toString());
			}
		} catch(Exception ex) {
			_parentAct.setLocationText("");
		}	
	}
	
	public GeoPoint getLocationPoint() {
		return _currentLocation;
	}
	
    public void setMapCenter()
    {
    	if (_parentAct.map() == null || _currentLocation == null)
    		return;

        MapController c = _parentAct.map().getController();

        c.setCenter(_currentLocation);
        c.animateTo(_currentLocation);
    }
	  
	@Override  
	public boolean onDoubleTap(MotionEvent e) {
		Projection projection = _parentAct.map().getProjection();
		setLocationPoint(projection.fromPixels((int)e.getX(), (int)e.getY()));
		_parentAct.map().invalidate();
		return false;  
	}  
	  
	@Override  
	public boolean onDoubleTapEvent(MotionEvent e) {
		return false;  
	}  
	  
	@Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow){

		if (_currentLocation == null) {
			return;
		}
		
		Projection projection = _parentAct.map().getProjection();
        Point point = new Point();
        projection.toPixels(_currentLocation, point);
        point.offset(_offsetIcon.x, _offsetIcon.y);

        // アイコンを描画
        canvas.drawBitmap(_iconLocation, point.x, point.y, null);
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onSingleTapConfirmed(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
}
